<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\V3_2_7\ScanCodeInStorageRequest;
use App\V3\V3_2_7\Models\StorageShelfLocationModel;
use App\V3\V3_2_7\Models\StorageShelfModel;
use App\V3\V3_2_7\Services\ScanCodeAllocateStorageService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ScanCodeAllocateStorageController extends BaseController
{
    /**
     *扫码调拨-孵化基地
     * Author: zlf
     * Created at 2024/6/27 15:50
     * @param ScanCodeInStorageRequest $request
     * @return JsonResponse
     */
    public function codeAllocateStorage(ScanCodeInStorageRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['time'] = time();
        $data['batch_title'] = $this->batchTitle();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('扫码调拨', $data, $data['time']);
        if (!empty($data['in_storage_id']) && empty($data['in_shelf_id']) && empty($data['in_location_id'])) {
            if (!empty($data['storage_id'])) {
                if ($data['in_storage_id'] == $data['storage_id'])
                    return fail('', '相同仓库的不能调拨！');
            }
            if (!empty($data['shelf_id'])) {
                $storageId = StorageShelfModel::query()
                    ->where('id', $data['shelf_id'])
                    ->value('storage_id');
                if ($data['in_storage_id'] == $storageId)
                    return fail('', '您选择货架商品的仓库与目标仓库相同，不能调拨！');
            }
            if (!empty($data['location_id'])) {
                $storageId = StorageShelfLocationModel::query()
                    ->where('id', $data['location_id'])
                    ->value('storage_id');
                if ($data['in_storage_id'] == $storageId)
                    return fail('', '您选择库位商品的仓库与目标仓库相同，不能调拨！');
            }
        }
        if (!empty($data['in_storage_id']) && !empty($data['in_shelf_id']) && empty($data['in_location_id'])) {
            if (!empty($data['storage_id'])) {
                $storageId = StorageShelfModel::query()
                    ->where('id', $data['in_shelf_id'])
                    ->value('storage_id');
                if ($data['storage_id'] == $storageId)
                    return fail('', '您选择仓库商品与目标货架的仓库相同，不能调拨！');
            }
            if (!empty($data['shelf_id'])) {
                if ($data['in_shelf_id'] == $data['shelf_id'])
                    return fail('', '相同货架的不能调拨！');
            }
            if (!empty($data['location_id'])) {
                $shelfId = StorageShelfLocationModel::query()
                    ->where('id', $data['location_id'])
                    ->value('shelf_id');
                if ($data['in_shelf_id'] == $shelfId)
                    return fail('', '您选择库位商品的货架与目标货架相同，不能调拨！');
            }
        }
        if (!empty($data['in_storage_id']) && !empty($data['in_shelf_id']) && !empty($data['in_location_id'])) {
            $storageInfo = StorageShelfLocationModel::query()
                ->where('id', $data['in_location_id'])
                ->first();
            if (!empty($data['storage_id'])) {
                if ($data['storage_id'] == $storageInfo['storage_id'])
                    return fail('', '您选择仓库商品与目标库位的仓库相同，不能调拨！');
            }
            if (!empty($data['shelf_id'])) {
                if ($data['shelf_id'] == $storageInfo['shelf_id'])
                    return fail('', '您选择货架商品与目标库位的货架相同，不能调拨！');
            }
            if (!empty($data['location_id'])) {
                if ($data['in_location_id'] == $data['location_id'])
                    return fail('', '相同库位的不能调拨！');
            }
        }
        return Cache::lock($data['type_id'] . __CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 5 秒并自动释放...
                return response()->json(ScanCodeAllocateStorageService::codeAllocateStorage($data));
            });
    }
}
